# -*- coding: utf-8 -*-
'''
Created on Jan 10, 2021

@author: yl
'''

from FFT_Interpolation import *
from WangJiaNing_FFT import x, tau0, window_blackman

# f = 6e6
f_set = np.linspace(3e6,7.5e6,num=100)
phi_set = np.linspace(0, 4*np.pi, num=100)

std_set = []

for f in f_set:
    result_set = []
    for phi in phi_set:
        sig = np.cos(2*np.pi*f*x + phi)
        # sig = sig * window_hamming
        sig = sig * window_blackman
#         plt.plot(sig)
#         plt.show()
        freqline, sig_FFT, sig_freq, sig_phase = FFT_cal(sig, tau0)
        # phase_result = sig_phase[sig_freq.argmax()]
        phase_result = sig_phase[3]
        result_set.append(phase_result)
    result_set = np.unwrap(result_set)
    diff = result_set - phi_set
#     diff = (np.max(diff) - np.min(diff))/2/np.pi*633/4
    std_nm = np.max(diff)/2/np.pi*633/4
    std_set.append(std_nm)
# print(np.max(diff)-np.min(diff), "rad")
# print((np.max(diff)-np.min(diff))/2/np.pi*633/4, "nm (四细分)") #四细分
# print(np.std(diff)/2/np.pi*633/4, "nm (四细分)")
print(std_set)
plt.plot(f_set/1e6,std_set,'b.-')

# plt.subplot(2,1,1)
# plt.plot(phi_set,'b')
# plt.plot(result_set,'r')
# plt.ylabel("Phase")
# plt.grid(which='major', axis='both')
# 
# plt.subplot(2,1,2)
# plt.plot(diff,'b', marker='o')
# plt.ylabel("Phase")
# plt.grid(which='major', axis='both')
# plt.tight_layout()
plt.show()